<template>
  <div class="component-distribution-user">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distributionMsg.distriTelPhone') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item :label="$t('distributionMsg.serialNumber') + ':'">
            <el-input
              v-model="searchForm.merchantOrderId"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.serialNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionMsg.withdrawalStatus') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('distributionMsg.withdrawalStatus')"
              clearable
            >
              <el-option
                :label="$t('distributionMsg.applying')"
                :value="0"
              />
              <el-option
                :label="$t('distributionMsg.withdrawalSuccessful')"
                :value="1"
              />
              <el-option
                :label="$t('distributionMsg.withdrawalFailed')"
                :value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distribution-withdrawCash-table">
        <el-table
          ref="distributionWithdrawCashTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <!-- 分销员昵称 -->
          <el-table-column
            width="120"
            prop="nickName"
            :label="$t('distribUserWallet.distributorNickname')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.nickName : '' }}
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            width="120"
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.userMobile : '' }}
            </template>
          </el-table-column>
          <!-- 流水号 -->
          <el-table-column
            prop="merchantOrderId"
            width="230"
            :label="$t('distributionMsg.serialNumber')"
          >
            <template #default="scope">
              {{ scope.row.merchantOrderId }}
            </template>
          </el-table-column>
          <!-- 金额 -->
          <el-table-column
            prop="amount"
            :label="$t('home.amount')"
            sortable
          >
            <template #default="scope">
              {{ scope.row.amount }}
            </template>
          </el-table-column>
          <!-- 提现时间 -->
          <el-table-column
            prop="createTime"
            :label="$t('distributionMsg.withdrawalTime')"
            sortable
            width="180"
          >
            <template #default="scope">
              {{ scope.row.createTime }}
            </template>
          </el-table-column>
          <!-- 提现类型 -->
          <el-table-column
            prop="type"
            :label="$t('distributionMsg.withdrawalType')"
          >
            <template #default="scope">
              <span v-if="scope.row.type === 0">{{ $t('distributionMsg.manualWithdrawal') }}</span>
              <span v-if="scope.row.type === 1">{{ $t('distributionMsg.automaticWithdrawal') }}</span>
            </template>
          </el-table-column>
          <!-- 提现状态 -->
          <el-table-column
            prop="state"
            :label="$t('distributionMsg.withdrawalStatus')"
          >
            <template #default="scope">
              <span v-if="scope.row.state === 0">{{ $t('distributionMsg.applying') }}</span>
              <span v-if="scope.row.state === 1">{{ $t('distributionMsg.withdrawalSuccessful') }}</span>
              <span v-if="scope.row.state === 2">{{ $t('distributionMsg.withdrawalFailed') }}</span>
            </template>
          </el-table-column>
          <!-- 更新时间 -->
          <el-table-column
            prop="updateTime"
            :label="$t('home.updateTime')"
            sortable
            width="180"
          >
            <template #default="scope">
              <span v-if="!scope.row.updateTime">无</span>
              <span v-else>{{ scope.row.updateTime }}</span>
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="130"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="
                    scope.row.state === 0 &&
                      isAuth('platform:distributionWithdrawCash:update')
                  "
                  class="default-btn text-btn"
                  @click="updateToSuccess(scope.row.withdrawCashId)"
                >
                  {{ $t('distribUserWallet.setWithdrawal') }}
                </div>
                <div v-else>
                  无
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>
<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
onMounted(() => {
  getDataList()
})

/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const sortParam = ref(3) // 0无 1金额 2手续费 3提现时间 4更新时间
const sortType = ref(2) // 0无 1 正序 2倒序
let tempSearchForm = null // 保存上次点击查询的请求条件
let theParams = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const dataList = ref([])
const dataListLoading = ref(false)
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm.value))
    tempSearchForm = {
      current: pageParam ? pageParam.currentPage : page.currentPage,
      size: pageParam ? pageParam.pageSize : page.pageSize,
      sortParam: sortParam.value,
      sortType: sortType.value
    }
  } else {
    tempSearchForm.current = pageParam ? pageParam.currentPage : page.currentPage
    tempSearchForm.size = pageParam ? pageParam.pageSize : page.pageSize
  }
  dataListLoading.value = true
  http({
    url: http.adornUrl('/platform/distributionWithdrawCash/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
    dataList.value = data.records
    dataListLoading.value = false
  })
}
/**
 * 新增 / 修改
 * @param withdrawCashId
 */
const updateToSuccess = (withdrawCashId) => {
  ElMessageBox.confirm($t('distribUserWallet.confirmSetWithdrawal'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/platform/distributionWithdrawCash/toSuccess/' + withdrawCashId),
      method: 'put'
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList()
        }
      })
    })
  })
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
</script>
